package com.swf.common.data;

import java.util.*;

import org.slf4j.*;

/**
 * description:集合类，存放FwsKeyedCollection
 * @version 1.0 2013-5-14
 * @author shenwf
 */
public class FwsIndexedCollection extends FwsDataElement implements List {
	public static long UNI_LIMIT_SIZE = -1L;
	public static long UNI_LIMIT_PACE = 100L;
	
	private long limitSize = -1L;
	
	private FwsDataElement dataElement = new FwsKeyedCollection();
	private List datas;
	private static Logger logger = LoggerFactory
			.getLogger(FwsIndexedCollection.class);
	
	public FwsIndexedCollection() {
		this.datas = new ArrayList();
	}
	public FwsIndexedCollection(String name) {
		super(name);
		this.datas = new ArrayList();
	}
	
	
	public FwsDataElement getElementAt(int idx){
		return getElementAt(idx,null);
	}
	public FwsDataElement getElementAt(int idx,Class appendClass){
		if (isAppend()&&(idx>=this.datas.size())){
			try {
				while ((this.datas.size()<=idx)&&(appendClass!=null)){
					FwsDataElement temp = (FwsDataElement)this.dataElement.clone();
					temp.setAppend(isAppend());
					this.datas.add(temp);
					checkLimit(temp);
				}
			} catch (Exception e) {
				logger.error("get(idx) auto Append dataElement in "+ getName(),e);
				return null;
			}
		}
		return (FwsDataElement) this.datas.get(idx);
	}
	public int size() {
		return this.datas.size();
	}
	
	public FwsDataElement removeElementAt(int idx){
		if (idx>=this.datas.size()){
			return null;
		}
		return (FwsDataElement) this.datas.remove(idx);
	}
	
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean contains(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	public Iterator iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	public Object[] toArray() {
		// TODO Auto-generated method stub
		return null;
	}

	public Object[] toArray(Object[] a) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean add(Object e) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean remove(Object o) {
		return this.datas.remove(o);
	}

	public boolean containsAll(Collection c) {
		return this.datas.contains(c);
	}

	public boolean addAll(Collection c) {
		for (Object o:c){
			if (o instanceof FwsDataElement){
				((FwsDataElement) o).setAppend(isAppend());
			}
		}
		boolean flag = this.addAll(c);
		checkLimit(c);
		return flag;
	}

	public boolean addAll(int index, Collection c) {
		for (Object o:c){
			if (o instanceof FwsDataElement){
				((FwsDataElement) o).setAppend(isAppend());
			}
		}
		boolean flag = this.addAll(index,c);
		checkLimit(c);
		return flag;
	}

	public boolean removeAll(Collection c) {
		return this.datas.removeAll(c);
	}

	public boolean retainAll(Collection c) {
		return this.datas.retainAll(c);
	}

	public void clear() {
		this.datas.clear();		
	}

	public Object get(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object set(int index, Object element) {
		// TODO Auto-generated method stub
		return null;
	}

	public void add(int index, Object element) {
		// TODO Auto-generated method stub
		
	}

	public Object remove(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	public int indexOf(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int lastIndexOf(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ListIterator listIterator() {
		// TODO Auto-generated method stub
		return null;
	}

	public ListIterator listIterator(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	public List subList(int fromIndex, int toIndex) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void checkLimit(Object input){
		long limit = -1L;
		if (this.limitSize>0L){
			limit = this.limitSize;
		}else{
			limit = UNI_LIMIT_SIZE;
		}
		if ((limit <= 0L) || (this.datas.size() < limit) ||
			((this.datas.size()-limit)%UNI_LIMIT_PACE!=0L) && (!(input instanceof Collection))	){
			return ;
		}
		String warnmsg = "warning:IndexedCollection["+getName()+"] size="+this.datas.size()
				+ limit + "]\nthe element is:\n"+input.toString()+"\n";
		logger.warn(warnmsg);
	}
	
}
